from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)
class Customer(models.Model):
    '''学生表-(客户信息表)'''
    name = models.CharField(max_length=32,blank=True,null=True)
    qq = models.CharField(verbose_name="学号",max_length=64,unique=True)
    qq_name = models.CharField(max_length=64,blank=True,null=True)
    phone = models.CharField(max_length=64,blank=True,null=True)
    source_choices = ((0,'同学介绍'),
                      (1,'QQ群'),
                      (2,'官网'),
                      (3,'强制'),
                      (4,'社团宣传'),
                      (5,'摆摊'),
                      (6,'微信公众号'),
                      )
    source = models.SmallIntegerField(choices=source_choices)
    referral_from = models.CharField(verbose_name="介绍人信息",max_length=64,blank=True,null=True)
    consult_course = models.ForeignKey("Course",verbose_name="咨询活动",on_delete=models.DO_NOTHING)
    content = models.TextField(verbose_name="咨询详情")
    tags = models.ManyToManyField("Tag",blank=True,null=True)
    status_choices = ((0, '已报名'),
                      (1, '未报名'),
                      )
    status = models.SmallIntegerField(choices=status_choices, default=1)
    consultant = models.ForeignKey("UserProfile",on_delete=models.DO_NOTHING)
    # 备注
    memo = models.TextField(blank=True,null=True)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "<%s %s>" % (self.qq, self.name)

    class Meta:
        verbose_name = "客户表"
        verbose_name_plural = "客户表"

class Tag(models.Model):
    name = models.CharField(unique=True,max_length=32)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = "标签"

class CustomerFollowUp(models.Model):
    '''客户跟进表'''
    customer = models.ForeignKey("Customer",on_delete=models.DO_NOTHING)
    content = models.TextField(verbose_name="跟进内容")
    consultant = models.ForeignKey("UserProfile",on_delete=models.DO_NOTHING)

    intention_choices = ((0,'考虑'),
                         (1,'拒绝'),
                         (2,'同意'),
                         (3,'无时间'),
                         (4,'已参加'),
                         (5,'已拉黑'),
                         )
    intention = models.SmallIntegerField(choices=intention_choices)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "<%s : %s>" %(self.customer.qq,self.intention)

    class Meta:
        verbose_name = "客户跟进记录"
        verbose_name_plural = "客户跟进记录"

class Course(models.Model):
    '''活动表-（课程表）'''
    name = models.CharField(max_length=64,unique=True)
    # 人数
    price = models.PositiveSmallIntegerField()
    period = models.PositiveSmallIntegerField(verbose_name="活动周期")
    # 活动介绍
    outline = models.TextField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "活动表"
        verbose_name_plural = "活动表"

class Branch(models.Model):
    '''学院表-（校区表）'''
    name = models.CharField(max_length=128,unique=True)
    addr = models.CharField(max_length=128)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "学院表"
        verbose_name_plural = "学院表"

class ClassList(models.Model):
    '''活动年次表-（班级表），e.g.2017年快乐班级，2018年快乐班级'''
    # 学院
    branch = models.ForeignKey("Branch",verbose_name="学院",on_delete=models.DO_NOTHING)
    course = models.ForeignKey("Course",on_delete=models.DO_NOTHING)
    class_type_choices = ((0,'体育'),
                          (1,'文艺'),
                          (2,'娱乐'),
                          )
    class_type = models.SmallIntegerField(choices=class_type_choices,verbose_name="活动类型")
    semester = models.PositiveSmallIntegerField(verbose_name="学期")
    teachers = models.ManyToManyField("UserProfile")
    start_date = models.DateField(verbose_name="开始日期")
    end_date = models.DateField(verbose_name="结束日期",blank=True,null=True)

    def __str__(self):
        return "%s %s %s" %(self.branch,self.course,self.semester)

    class Meta:
        unique_together = ('branch','course','semester')
        verbose_name_plural = "活动年次表"
        verbose_name = "活动年次表"



class CourseRecord(models.Model):
    '''活动记录表-（上课记录表）'''
    from_class = models.ForeignKey("ClassList",verbose_name="活动",on_delete=models.DO_NOTHING)
    day_num = models.PositiveSmallIntegerField(verbose_name="第几天")
    teacher = models.ForeignKey("UserProfile",on_delete=models.DO_NOTHING)
    # 有无作业
    has_homework = models.BooleanField(default=True)
    # 作业标题
    homework_title = models.CharField(max_length=128,blank=True,null=True)
    # 作业内容
    homework_content = models.TextField(blank=True,null=True)
    # 活动概要
    outline = models.TextField(verbose_name="活动概要")
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return "%s %s" %(self.from_class,self.day_num)

    # 唯一
    class Meta:
        unique_together = ("from_class","day_num")
        verbose_name_plural = "活动记录表"
        verbose_name = "活动记录表"

class StudyRecord(models.Model):
    '''学生记录表-（学习记录表）'''
    student = models.ForeignKey("Enrollment",on_delete=models.DO_NOTHING)
    course_record = models.ForeignKey("CourseRecord",on_delete=models.DO_NOTHING)
    attendance_choices = ((0,'已签到'),
                          (1,'迟到'),
                          (2,'缺勤'),
                          (3,'早退'),
                          )
    attendance = models.SmallIntegerField(choices=attendance_choices,default=0)
    score_choices = ((100,"A+"),
                     (90,"A"),
                     (85,"B+"),
                     (80,"B"),
                     (75,"B-"),
                     (70,"C+"),
                     (60,"C"),
                     (40,"C-"),
                     (-50,"D"),
                     (-100,"COPY"),
                     (0,"N/A"),
                     )
    score = models.SmallIntegerField(choices=score_choices)
    memo = models.TextField(blank=True,null=True)
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return "%s %s %s" %(self.student,self.course_record,self.score)

    class Meta:
        unique_together = ('student','course_record')
        verbose_name_plural = "学生记录"
        verbose_name = "学生记录"

class Enrollment(models.Model):
    '''报名表- 存参加活动的学生，及其参加的活动信息，存与活动相关联的信息，存学生参加的活动信息，不存学生基本信息'''
    customer = models.ForeignKey("Customer",on_delete=models.DO_NOTHING)
    enrolled_class = models.ForeignKey("ClassList",verbose_name="所参加活动",on_delete=models.DO_NOTHING)
    consultant = models.ForeignKey("UserProfile",verbose_name="负责人",on_delete=models.DO_NOTHING)
    contract_agreed = models.BooleanField(default=False,verbose_name="学生报名参加活动")
    contract_approved = models.BooleanField(default=False,verbose_name="同意报名")
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s" %(self.customer,self.enrolled_class)

    # 联合唯一
    class Meta:
        unique_together = ("customer","enrolled_class")
        verbose_name_plural = "报名表"
        verbose_name = "报名表"

class Payment(models.Model):
    '''登记记录-(缴费记录)'''
    customer = models.ForeignKey("Customer",on_delete=models.DO_NOTHING)
    course = models.ForeignKey("Course",verbose_name="所报名活动",on_delete=models.DO_NOTHING)
    amount = models.PositiveIntegerField(verbose_name="缴费金额")
    consultant = models.ForeignKey("UserProfile",on_delete=models.DO_NOTHING)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s" %(self.customer,self.amount)

    class Meta:
        verbose_name_plural="登记表"
        verbose_name="登记表"

class UserProfile(models.Model):
    '''账号表-用户表'''
    user = models.OneToOneField(User,on_delete=models.DO_NOTHING)
    name = models.CharField(max_length=32)
    roles = models.ManyToManyField("Role",blank=True,null=True)
    stu_accout = models.ForeignKey("Customer",verbose_name="关联学生账号",blank=True,null=True,on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural="用户表"
        verbose_name="用户表"


class Role(models.Model):
    '''角色表'''
    name = models.CharField(max_length=32,unique=True)
    menus = models.ManyToManyField("Menu",blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "角色表"
        verbose_name = "角色表"


class Menu(models.Model):
    '''菜单表'''
    name = models.CharField(max_length=32)
    url_type_choices = ((0,'alias'),(1,'absolute_url'))
    url_type = models.SmallIntegerField(choices=url_type_choices,default=0)
    url_name = models.CharField(max_length=64)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural="菜单表"
        verbose_name="菜单表"